/**
 * Predefined variables
 * Name = RegisterLayer
 * DateTime = Sun Jan 16 2022 22:37:17 GMT+0800 (中国标准时间)
 * Author = Created by zhj9922
 * FileBasename = RegisterLayer.ts
 * gitee： https://gitee.com/coyeking
 */
import { audioManager } from '../../framework/audioManager';
import { UiBase } from '../uiBase';
 
import { _decorator,EditBox,director } from 'cc';
const { ccclass, property } = _decorator;
import { verify } from '../../utils/verify';
import { uiManager } from '../../framework/uiManager';
import { ModeluType } from '../../define/ccenum';
import { httpApi } from '../../api/httpApi';
import { userGlobal } from '../../global/userGlobal';
 
@ccclass('RegisterLayer')
export class RegisterLayer extends UiBase {
    @property(EditBox)
    public ui_accontEdit: EditBox = null;
    
    @property(EditBox)
    public ui_pwdEdit: EditBox = null;
	
    onEnable() {
        super.onEnable();
    }
	
    /**
     * 按钮点击事件
     * @param event 
     * @param customEventData 
     */
	onClick (event: Event, customEventData: string) {
        audioManager.instance.playSound();

        if (customEventData == 'close'){
            this.closeAction();
        }else{
            this.onRegister();
        }
    }
    onRegister(){
        let _this = this;
        if (!this.ui_accontEdit.string || !verify.enOrNum(this.ui_accontEdit.string)){
            uiManager.showToast('账号有误,请重新输入',ModeluType.FAIL);
            return;
        };
        if (!this.ui_pwdEdit.string){
            uiManager.showToast('请输入密码',ModeluType.FAIL);
            return;
        };
        uiManager.showWaiting(true);

        httpApi.register({username:this.ui_accontEdit.string,password:this.ui_pwdEdit.string},(result)=>{
            uiManager.showWaiting(false);
            uiManager.showToast(result.msg,ModeluType.SUCCESS);
            userGlobal.instance.setUserInfo(result.data.userinfo);
            setTimeout(function(){
                director.loadScene("hallscence");
            },1000); 
        },(res)=>{
            uiManager.showWaiting(false);
            uiManager.showToast(res.msg,ModeluType.FAIL);
        });


    }
}

